<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Menu
 *
 * @author Acer
 */
class Content_Model_Admin_Menu extends MVC_Model{
    
    public function parentChildContentMenu()
    {   
        $conditions = array();
        
        if(fRequest::get('filter[id]') != '')
        {
            $conditions[] = '`id` = '.fRequest::get('filter[id]');
        }
        else
        {
            if(fRequest::get('filter[menu_title]') != '')
            {
                $conditions[] = sprintf('`menu_title` LIKE "%%s%"', fRequest::get('filter[menu_title]'));
            }
            
            if(fRequest::get('filter[menu_position]') != '')
            {
                $conditions[] = sprintf('`menu_position` = "%s"', fRequest::get('filter[menu_position]'));
            }
            
            if(fRequest::get('filter[permalink]') != '')
            {
                $conditions[] = sprintf('`permalink` LIKE "%%s%"', fRequest::get('filter[permalink]'));
            }
            
            if(fRequest::get('filter[sorted]') != '')
            {
                $conditions[] = sprintf('`sorted` = "%s"', fRequest::get('filter[sorted]'));
            }
            
            if(fRequest::get('filter[home_page]') != '')
            {
                $conditions[] = sprintf('`home_page` = "%s"', fRequest::get('filter[home_page]'));
            }
            
            if(fRequest::get('filter[posted]') != '')
            {
                $conditions[] = sprintf('`posted` = "%s"', fRequest::get('filter[posted]'));
            }
            
        }
        $menuCount = $this->db->getBy('content_menu', 
                                        array_merge(array('`parent_id` = 0'), 
                                                    $conditions
                                                   ), 
                                        array('ORDER BY `sorted`'));
        
        $parentMenus = $this->db->getBy('content_menu', 
                                        array_merge(array('`parent_id` = 0'), 
                                                    $conditions
                                                   ), 
                                        array('ORDER BY `sorted`', 'LIMIT ' . (($this->pagination->get_page() - 1) * $this->records_per_page) . ', ' . $this->records_per_page));
        
        $rows = $this->db->getRecord('SELECT FOUND_ROWS() AS rows');
        
        
        $this->pagination->records(count($menuCount));
        
        $this->pagination->records_per_page($this->records_per_page);
        
        if(Util::checkArray($parentMenus))
        {
            foreach($parentMenus as $k => $parentMenu)
            {
                $childMenus = $this->db->getBy('content_menu', array_merge(array('`parent_id` = '.$parentMenu['id']), $conditions), array('ORDER BY `sorted`'));

                $parentMenus[$k]['children'] = $childMenus;
            }
        }
        else
        {
            $parentMenus = array();
        }
        
        return $parentMenus;
    }
}
